package com.bw.shop2208a.login

import android.content.Intent
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.ContactsContract.CommonDataKinds.Phone
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.constant.RegexConstants.REGEX_USERNAME
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.shop2208a.R
import com.bw.shop2208a.WebViewActivity
import com.bw.shop2208a.databinding.ActivityRegisterBinding
import com.bw.shop2208a.entity.RegisterParam
import com.bw.shop2208a.mvvm.BaseActivity
import com.bw.shop2208a.utils.Constant
@Route(path = "/app/register")
class RegisterActivity : BaseActivity<ActivityRegisterBinding, UserViewModel>() {
    override fun initViwe() {
        //给复选框添加监听事件
        binding.box.setOnCheckedChangeListener { buttonView, isChecked ->
            binding.btn1.isEnabled = binding.box.isChecked
        }

        binding.btn1.setOnClickListener {
            val pass = binding.pass.text.toString()
            val rpass = binding.rpass.text.toString()
            val user = binding.user.text.toString()
            if (pass.isNotEmpty()||rpass.isNotEmpty()||user.isNotEmpty()){
                if (!RegexUtils.isUsername(user)){
                    ToastUtils.showLong("用户名不合法，需要永久坐牢")
                    return@setOnClickListener
                }else if (!RegexUtils.isMatch(REGEX_USERNAME,pass)){
                    ToastUtils.showLong("密码不符合要求")
                    return@setOnClickListener
                } else{
                    val registerParam = RegisterParam(
                        "","","","","",pass,rpass,"",user
                    )
                    viewModel.register(registerParam)
                }
            }else{
                ToastUtils.showLong("请输入内容")
            }
        }

        val stringBuilder = SpannableStringBuilder(binding.box.text)
        stringBuilder.setSpan(ForegroundColorSpan(Color.RED),6,12,1)
        stringBuilder.setSpan(ForegroundColorSpan(Color.RED),13,19,1)
        stringBuilder.setSpan(object :ClickableSpan(){
            override fun onClick(widget: View) {
                val intent = Intent(this@RegisterActivity, WebViewActivity::class.java)
                intent.putExtra("web_url","https://m.qfang.com/shenzhen/user/user-protocol.html")
                startActivity(intent)
            }
        },6,12,1)
        stringBuilder.setSpan(object :ClickableSpan(){
            override fun onClick(widget: View) {
                val intent = Intent(this@RegisterActivity, WebViewActivity::class.java)
                intent.putExtra("web_url","https://m.qfang.com/shenzhen/user/privacy-protocol.html")
                startActivity(intent)
            }
        },13,19,1)
        binding.box.setText(stringBuilder)
        binding.box.movementMethod = LinkMovementMethod.getInstance()
    }

    override fun iniData() {
        viewModel.success_register.observe(this){
            ToastUtils.showLong(it.message)
            if (it.code==200){
                val intent = Intent(this, LoginActivity::class.java)
                intent.putExtra("user",it.data.username)
                intent.putExtra("pass",it.data.password)
                setResult(102,intent)
                finish()
            }
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_register

    override fun getViewModel(): Class<UserViewModel> = UserViewModel::class.java

}